Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X11] Partial fix for Editor and Project Manager stealing focus on some window managers #86101

Merged
merged 1 commit into from
Mar 11, 2024
Merged

Conversation

GrammAcc
Copy link
Contributor

This is a workaround for the most critical portion of the WM focus bug described in #68305. On some specific X11 WM configurations, the editor's main window and any popups it creates will fight for focus, which causes a total system lockup due to mouse and keyboard input being stolen as well. Getting out of this infinite loop requires force restarting the system.

It can be tested with the following shell script:

!#/bin/sh

godot4 &
sleep 30
pkill -x godot4

Here is a gif of the bug in action on my machine:

68305-bug

The gif above actually doesn't show the exact behavior. The constant focus swap is actually too fast for my screen recorder's framerate to pick up, so what you are seeing is about 24 out of 60 focus swaps. :)

Here is the same with this patch applied:

68305-fixed

My system information:

  • Linux version: 6.6.6-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0)
  • WM: Fluxbox-only launched via Xinit.

The workaround identified by @nargacu83 in #68305 is to remove the call to XSetInputFocus in the ConfigureNotify event handler, so I have removed the conditional block that calls this as well as the setup code above it since there is no need to allocate the memory for the variables if they won't be used in that call anymore.

This is just a hack and is not a complete fix for #68305. Multiple developers are collaborating on a proper fix in the discussion in that issue, but time is a valuable resource that no one has enough of, so I am submitting this workaround as a stop-gap to prevent the most critical problem while we work on a full solution for the underlying cause.

This bug affects a very small number of users, and the general consensus in the related issue is that we don't want to submit a PR for a hack if we can get a proper fix. Particularly since we aren't completely sure why this workaround fixes the problem, and I agree with this as well, but personally, I am concerned that with this being a platform bug, it could affect exported games and not just developers, and in that case, it probably wouldn't show up on the developer's machine, but a player with the affected configuration could encounter the bug when running an exported Godot game, and although the number of people playing games on custom WM-only Linux machines is probably very low, it only takes one very vocal gamer on Twitter to ruin an indie studio, so I wanted to submit this workaround now instead of sitting on it since no one in the related issue has reported any regressions with this workaround, and we don't know when a proper fix will be ready.

Please feel free to close this PR if you feel that we should wait until we have a better idea of the problem and exactly why this workaround works. Thanks!

@bruvzg
Copy link
Member

bruvzg commented Dec 13, 2023

For the reference, it was added in #41456, and issues with freezing on some WMs were reported soon after #41574, but it's marked as resolved (probably only fixed with the i3 specifically).

@akien-mga akien-mga changed the title Platform-X11: Partial Fix Godot Editor and Project Manager steals focus on a window manager on Linux [X11] Partial fix for Editor and Project Manager stealing focus on some window managers Dec 13, 2023
@akien-mga akien-mga added bug platform:linuxbsd topic:porting cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Dec 13, 2023
@akien-mga akien-mga added this to the 4.3 milestone Dec 13, 2023
@akien-mga
Copy link
Member

akien-mga commented Dec 13, 2023

What will be the consequence of removing that code on WMs which don't have that focus stealing bug?

In other words, what is the original code meant to do?

@bruvzg
Copy link
Member

bruvzg commented Dec 13, 2023

Popup handling was changed a lot, so I'm not sure if this code is necessary for anything. It should be tested to see if it's reintroducing #37674 or #41578 on non tiling WMs.

@akien-mga
Copy link
Member

Popup handling was changed a lot, so I'm not sure if this code is necessary for anything. It should be tested to see if it's reintroducing #37674 or #41578 on non tiling WMs.

Tested briefly on KDE Plasma on X11, it doesn't reintroduce those issues for me. From a quick test I didn't spot any weird change in behavior.

Would be good to have it tested on more DEs/WMs and on Xwayland.

CC @Riteo @groud @Calinou

@GrammAcc
Copy link
Contributor Author

Thanks for taking the time to review this!

I'm in code review at work this week, so I actually have some extra free time in the evenings until this weekend. I can install a lot of different DEs on my Arch box, so I will try to test this against some other popular DEs and WMs after work this week and report the results. :)

@dsnopek
Copy link
Contributor

dsnopek commented Dec 13, 2023

As another random data point, I use i3 and Godot's "window grabbing focus behavior" is super disruptive. However, in my case, it doesn't lead to multiple windows fighting for focus and things getting locked up, it's just very annoying. :-) When I have a chance I'll try with this PR and see if it helps in my case. But it would be kind of nice to have an editor setting that I could enable that would just prevent any window in the editor from grabbing focus.

@GrammAcc
Copy link
Contributor Author

As another random data point, I use i3 and Godot's "window grabbing focus behavior" is super disruptive. However, in my case, it doesn't lead to multiple windows fighting for focus and things getting locked up, it's just very annoying. :-) When I have a chance I'll try with this PR and see if it helps in my case. But it would be kind of nice to have an editor setting that I could enable that would just prevent any window in the editor from grabbing focus.

In case it helps, #74378 is related to #68305, but has a broader scope, and users have reported odd focus behavior between workspaces on i3. I generally only use one workspace, so I haven't looked into this, but one user confirmed that this workaround does help with focus on i3 but doesn't fix the issue between workspaces. See #68305 (comment) for details.

Confirmation from another i3 user would be really helpful though. :)

@dsnopek
Copy link
Contributor

dsnopek commented Dec 13, 2023

I just tested this PR with i3, and things don't seem to get any worse, but it doesn't seem to help my problems either.

To be clear, my issues mostly revolve around switching between the Godot editor and VS Code, where Godot will grab (or refuse to give up) input focus, and despite a new window visually appearing to have focus (like, the title bar is highlighted in the way that usually indicates it has focus), all the keyboard input still goes into the Godot editor. My "work around" is that I have to toggle back and forth between focusing on Godot and focusing on the thing I actually want to get my keyboard inputs, and eventually they'll go where I want.

I'll try to see if my problem is represented in one of the existing issues, and if not, make a new issue.

@GrammAcc
Copy link
Contributor Author

I just tested this PR with i3, and things don't seem to get any worse, but it doesn't seem to help my problems either.

To be clear, my issues mostly revolve around switching between the Godot editor and VS Code, where Godot will grab (or refuse to give up) input focus, and despite a new window visually appearing to have focus (like, the title bar is highlighted in the way that usually indicates it has focus), all the keyboard input still goes into the Godot editor. My "work around" is that I have to toggle back and forth between focusing on Godot and focusing on the thing I actually want to get my keyboard inputs, and eventually they'll go where I want.

I'll try to see if my problem is represented in one of the existing issues, and if not, make a new issue.

Yeah, there are a lot of problems with the focus behavior on WMs in the X11 backend. This PR is only intended to address the system lock up on very specific configurations since it seems like a fix for the other focus issues will require a significant overhaul of the event handlers, and a few devs have been working on this off and on for months without a lot of progress (mostly due to lack of time). I think some of the comments in #74378 mentioned similar issues with i3, and the user who tested this workaround on i3 previously did mention something similar about focus problems with an external text editor.

Thanks for testing it out!

@GrammAcc
Copy link
Contributor Author

GrammAcc commented Dec 14, 2023

Edit: Removed the other gifs as they all show the exact same behavior and they were causing the issue to load slowly and also burning my eyes. :)

Okay, I was able to test this on the following WMs/DEs: Fluxbox with picom (Xwayland), Openbox X11, Openbox Xwayland, Budgie, Cinnamon, Enlightenment, lxde, lxqt over Fluxbox, lxqt over Openbox, Mate, and xfce4.

I just did a quick test of launching the editor and opening a popup, so I have not tested for regressions related to #37674 and #41578 yet, but I wanted to make sure there wasn't anything weird going on with popups before regression testing on each DE.

I will check for #37674 and #41578 on each DE eventually this week, but it probably won't be tonight. :)

The following is a gif of the test on Fluxbox with an Xwayland compositor. The behavior was exactly the same on the other environments, so I am only including the single gif, but I can upload the others if you want.

Xwayland-fluxbox

@GrammAcc
Copy link
Contributor Author

Okay, I took a look at #37674 and #41578 and they were much simpler to test than I thought. 🤦‍♂️

I tested both on all of these environments: Fluxbox X11, Fluxbox Xwayland, Openbox X11, Openbox Xwayland, Budgie, Cinnamon, Enlightenment, lxde, lxqt over Fluxbox, lxqt over Openbox, Mate, and xfce4.

The following is a gif of the test on Fluxbox X11. All of the other environments gave exactly the same behavior, so I am only uploading the single gif:

regression-test-fluxbox

I believe this is everything I can test as far as different DEs goes. Please let me know if you have any questions for me. Thanks!

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally (Fedora 39 KDE with X11, rebased against 96296e4), it doesn't improve the situation with regards to focus stealing on floating window managers. The issue described in #86441 (review) still occurs with this PR, unfortunately.

That said, with this PR cherry-picked on top of #86441, the issue reported in the other PR remains fixed. I tested the editor quickly and couldn't notice any regressions in editor popup behavior. As a result, we can still choose to merge this if it helps improve the situation on tiling WMs.

@GrammAcc
Copy link
Contributor Author

GrammAcc commented Feb 5, 2024

Just a heads up, I wanted to check and see if #86441 and #86671 fixed this issue as well, so I tested the latest master, but I still get the system lockup on my machine.

Here's a recording of the behavior on current master:

screen-1_2024-02-05_13:19:38

Interestingly, the focus swap is slowed down considerably from before, but it's still faster than my 24fps screen recorder can keep up with, and I still can't get any input to register. I'm guessing the slower focus swapping is due to @dsnopek's fixes reducing the number of extraneous X11 events handled by Godot, but I can't say for sure, and it's still continuous.

The patch provided in this PR still fixes the lockup on my system, so I think this PR is still necessary at this point.

Lmk if there is anything else I can help with review-wise. Thanks!

@akien-mga
Copy link
Member

This needs a rebase against the current master branch to resolve conflicts. And then I guess we should just give it a spin and see if any X11 users report any regression.

…window manager on Linux

This is a workaround for the most critical portion of the WM focus bug
described in #68305. On some specific X11 WM configurations, the
editor's main window and any popups it creates will fight for focus,
which causes a total system lockup due to mouse and keyboard input being
stolen as well. Getting out of this infinite loop requires force
restarting the system.

It can be tested with the following shell script:

```bash
	!#/bin/sh

	godot4 &
	sleep 30
	pkill -x godot4
```

The workaround identified in #68305 is to remove the call to
XSetInputFocus in the ConfigureNotify event handler, so I have removed
the conditional block that calls this as well as the setup code above it
since there is no need to allocate the memory for the variables if they
won't be used in that call anymore.

This is just a hack and is not a complete fix for #68305. Multiple
developers are collaborating on a proper fix in the discussion in that
issue, but time is a valuable resource that no one has enough of, so I
am committing this workaround as a stop-gap to prevent the most critical
problem while we work on a full solution for the underlying cause.
@GrammAcc
Copy link
Contributor Author

GrammAcc commented Feb 5, 2024

Rebased over d335281 Thanks!

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it out!

@akien-mga akien-mga merged commit 058202e into godotengine:master Mar 11, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@GrammAcc GrammAcc deleted the partial-fix-68305 branch March 11, 2024 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release platform:linuxbsd topic:porting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants